#!/usr/bin/perl
# Version 0.2.1

$testcase = "dd_mtu_match";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 3) {
        print("Usage: $testcase.pl dump_file1 ... dump_file3 log_file");
        exit 1;
}

$dumpfile[0] = $ARGV[0];
$dumpfile[1] = $ARGV[1];
$dumpfile[2] = $ARGV[2];
$logfile = $ARGV[3];

#------------------------------------------------------------------------------

@local_verdict = ( 3, 3, 3 );

$success = 1;

$initial = 1;
# first check the first scenario
open(DUMPFILE, $dumpfile[0]) or $success = 0;

if($success eq 1) {
	# look at the dd packets, originated by 0.0.0.17
	while(<DUMPFILE>) {
		if (/OSPFv2-dd/) {
			if ($_ =~ /0\.0\.0\.17 > /) {
				if( $_ =~ /backbone E I/ ) {
					if($initial eq 1) {
						$local_verdict[0] = 1;
						$initial = 0;
					}
				} else {
					$local_verdict[0] = 2;
				}
			}
		}
	}
	close (DUMPFILE);
}

$success = 1;

$initial = 1;
# second scenario
open(DUMPFILE, $dumpfile[1]) or $success = 0;

if($success eq 1) {
	# look at the dd packets, originated by 0.0.0.10
	while(<DUMPFILE>) {
		if (/OSPFv2-dd/) {
			if ($_ =~ /0\.0\.0\.10 > /) {
				if( $_ =~ /backbone E I/ ) {
					if($initial eq 1) {
						$local_verdict[1] = 1;
						$initial = 0;
					}
				} else {
					$local_verdict[1] = 2;
				}
			}
		}
	}
	close (DUMPFILE);
}

$success = 1;

$initial = 2;
# third scenario
open(DUMPFILE, $dumpfile[2]) or $success = 0;

if($success eq 1) {
	# look at the dd packets, originated by 0.0.0.1
	while(<DUMPFILE>) {
		if (/OSPFv2-dd/) {
			if ($_ =~ /0\.0\.0\.1 > /) {
				if($initial > 0) {
					$initial--;
					if( $_ =~ / I| MS|\/MS]/ ) {
						$local_verdict[2] = 2;
					} else {
						$local_verdict[2] = 1;
					}
				}
			}
		}
	}
	close (DUMPFILE);
}


# finally evaluate results and generate report
if ($local_verdict[0] eq 1) {
	print("Packets with MTU set too high are dropped by slave. Test PASSED.\n");
} elsif ($local_verdict[0] eq 2) {
	print("Packets with MTU set too high are not dropped by slave. Test FAILED.\n");
} else {
	print("Test INCONCLUSIVE.\n");
}

if ($local_verdict[1] eq 1) {
	print("Packets with MTU set too high are dropped by master. Test PASSED.\n");
} elsif ($local_verdict[1] eq 2) {
	print("Packets with MTU set too high are not dropped by master. Test FAILED.\n");
} else {
	print("Test INCONCLUSIVE.\n");
}

if ($local_verdict[2] eq 1) {
	print("Packets with lower MTU are not dropped. Test PASSED.\n");
} elsif ($local_verdict[2] eq 2) {
	print("Packets with lower MTU are dropped. Test FAILED.\n");
} else {
	print("Test INCONCLUSIVE.\n");
}


$verdict = $local_verdict[0];
if ($local_verdict[1] ne $verdict) {
	if($local_verdict[1] eq 2) {
		$verdict = 2;
	} elsif ($local_verdict[1] eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}
if ($local_verdict[2] ne $verdict) {
	if($local_verdict[2] eq 2) {
		$verdict = 2;
	} elsif ($local_verdict[2] eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");